import { Request } from "./request"
import type { AuthRequest, TokenResponse } from "./Type/Token"
import type { MessageResultResponse, MessageResultResponseErr, postDevicesMessageRequest } from "./Type/DevicesMessage"
import type { DeviceShadowResponse } from "./Type/DeviceShadow"
import type { DeviceListResponse } from "./Type/DevicesList"

export const getToken = async () => {
  const axios = new Request('https://iam.cn-north-4.myhuaweicloud.com', {
    isToken:false
  })

  const str: AuthRequest = {
    "auth": {
      "identity": {
        "methods": ["password"],
        "password": { "user": { "name": "car", "password": "123456mc", "domain": { "name": "zjh6014" } } }
      },
      "scope": { "project": { "name": "cn-east-3" } }
    }
  }

  return await axios.post<TokenResponse>('/v3/auth/tokens', str)
}

const axios = new Request('https://7acc11ea0d.st1.iotda-app.cn-north-4.myhuaweicloud.com')

/**
 * 获取产品列表
 */
// export const getProductsList = (projectID: string) => axios.get<>(`/v5/iot/${projectID}/products`)

/**
 * 获取设备列表
 */
export function getDevicesList(projectID: string) {
  console.log("获取设备列表")
  return axios.get<DeviceListResponse>(`/v5/iot/${projectID}/devices`)
}
/**
 * 获取设备影子数据
 */
export function getDeviceShadow(project_id: string, device_id: string) {
  console.log("<请求>获取设备影子数据")
  return axios.get<DeviceShadowResponse | MessageResultResponseErr>(`/v5/iot/${project_id}/devices/${device_id}/shadow`)
}

/**
 * 下发设备消息
 */
export function sendDevicesMessage(project_id: string, device_id: string, data: postDevicesMessageRequest) {
  console.log("<请求>下发设备消息")
  return axios.post<MessageResultResponse | MessageResultResponseErr>(`/v5/iot/${project_id}/devices/${device_id}/messages`, data)
}